package com.andnnl.utils;

import java.io.UnsupportedEncodingException;


public class BytesUtils {

	

	
	//4byte数组转成long 
    public static long byte4ToLong(byte[] b) { 
        long s = 0; 
        long s0 = b[0] & 0xff;
        long s1 = b[1] & 0xff; 
        long s2 = b[2] & 0xff; 
        long s3 = b[3] & 0xff; 
        s0 <<= 24;
        s1 <<= 16; 
        s2 <<= 8; 
        s = ((long)s0 | s1 | s2 | s3 )&0xFFFFFFFFl;
        return s; 
    } 
	
	
	/**
	 * 转换字符串去指定长度的字节数组 字节数组长度不能小于字符串转换后的长度
	 * @param data
	 * @param charsetName
	 * @param length
	 * @return
	 */
	public static byte[] convertString2Bytes(String data,String charsetName,int length)
	{
		try
		{
			byte[] bytes = data.getBytes(charsetName);
			int copyLength = bytes.length;
			byte[] result = new byte[length];
			copyBytes(bytes, result, 1, copyLength, 1);
			return result;
		} catch (UnsupportedEncodingException e)
		{
			e.printStackTrace();
		}
		return new byte[length];
	}
	
	public static byte[] convertString2Bytes(String data,int length)
	{
		return convertString2Bytes(data, "UTF-8", length);
	}
	
	/**
	 * 获取int类型低位1字节
	 * @param data
	 * @return
	 */
	public static byte convertInt2Byte(int data)
	{
		return (byte)(0xFF & data);
	}
	
	public static byte[] convertInt2Bytes(int data)
	{
		byte[] bytes = new byte[]{convertInt2Byte(data)};
		return bytes;
	}
	
	
	/**
	 * 获取long类型 低位4字节
	 * @param data
	 * @return
	 */
	public static byte[] convertLong2FourBytes(long data)
	{
		byte[] result = new byte[4];
		result[3] = ((byte) (0xFF & data));
		result[2] = ((byte) ((0xFF00 & data) >> 8));
		result[1] = ((byte) ((0xFF0000 & data) >> 16));
		result[0] = ((byte) ((0xFF000000 & data) >> 24));
		return result;
	}

	
	
	/**
	 * 复制字节
	 * @param source
	 * @param dest
	 * @param sourceFrom  从1开始
	 * @param copyLength 需要复制的长度
	 * @param destFrom 从1开始
	 */
	public static void copyBytes(byte[] source,byte[] dest,int sourceFrom,int copyLength,int destFrom)
	{
		sourceFrom = sourceFrom -1;
		destFrom =  destFrom - 1;
		for (int i = destFrom; i < destFrom + copyLength; i++)
		{
			dest[i] = source[sourceFrom++];
		}
	}
//	
//	public static byte[] mergeBytes(byte[]... bytes)
//	{
//		byte[] temp = new byte[0];
//		for (int i = 0; i < bytes.length; i++)
//		{
//			temp = ArrayUtils.addAll(temp, bytes[i]);
//		}
//		return temp;
//	}
//	
//	public static byte[] mergeBytes(List<byte[]> list)
//	{
//		byte[] temp = new byte[0];
//		if(null != list)
//		{
//			for (byte[] bs : list)
//			{
//				temp = ArrayUtils.addAll(temp, bs);
//			}
//		}
//		return temp;
//	}
//	

	public static void main(String[] args)
	{
		// System.out.println(Arrays.toString(convertLong2FourBytes(0x1l)));
		//System.out.println(getCurrentDate());
		/*byte[] source = new byte[]{1,2,3,4};
		byte[] dest = new byte[12];
//		copyBytes(source, dest, 1, 4, 9);
//		System.out.println(Arrays.toString(dest));
		
		byte[] source2 = new byte[]{3,4,7,7};
		
		byte[] source3 = new byte[]{7,7,9,9};
		List<byte[]> list = null;
		dest = mergeBytes(list);
		dest = mergeBytes(source,source2,source3);
		System.out.println(Arrays.toString(dest));*/
		
		/*long d = 1102020202l;
		System.out.println(Long.toBinaryString(d));
		byte[] bs = convertLong2FourBytes(d);
		System.out.println(Arrays.toString(bs));
		
		System.out.println(byte4ToLong(bs));
		*/
		//byte b = -4;
		
		//System.out.println(convertUnsignByte2Int(b));


		byte[] source2 = new byte[]{123,4,7,7};
		for (byte a : source2) {
			System.out.println(a);
		}

		String s=bytesToHexStr(source2);
		System.out.println(s);
		byte[] source0=toByteArray(s);
		for (byte a : source0) {
			System.out.println(a);
		}
		System.out.println(source0);
	}


	public static int convertUnsignByte2Int(byte b)
	{
		int i = (b & 0xFF);
		return i;
	}
	
	
	/**
     * 16进制的字符串表示转成字节数组
     *
     * @param hexString
     *            16进制格式的字符串
     * @return 转换后的字节数组
     **/
    public static byte[] toByteArray(String hexString) {
        if (hexString==null || "".equals(hexString.trim()))
            throw new IllegalArgumentException("this hexString must not be empty");

        hexString = hexString.toLowerCase().replaceAll(" ","");
        final byte[] byteArray = new byte[hexString.length() / 2];
        int k = 0;
        for (int i = 0; i < byteArray.length; i++) {//因为是16进制，最多只会占用4位，转换成字节需要两个16进制的字符，高位在先
            byte high = (byte) (Character.digit(hexString.charAt(k), 16) & 0xff);
            byte low = (byte) (Character.digit(hexString.charAt(k + 1), 16) & 0xff);
            byteArray[i] = (byte) (high << 4 | low);
            k += 2;
        }
        return byteArray;
    }
    
    /**
  * 功能：
  *     将字节转换为16进制码（在此只是为了调试输出，此函数没有实际意义）
  * @param b  
  * @return 转化后的16进制码
  * @Author: eric(eric_cheung709@hotmail.com)
     * created in 2007/04/28 16:33:06
  */
    public static String bytesToHexStr(byte[]b) 
    {
        if (b == null)return "";
        StringBuffer strBuffer = new StringBuffer(b.length * 3);
        for (int i = 0; i < b.length; i ++ ) 
        {
        	String s=Integer.toHexString(b[i] & 0xff);
        	if(s.length()==1){
        		strBuffer.append("0");
        	}
            strBuffer.append(s);
            strBuffer.append(" ");
        }
        return strBuffer.toString();
    }
	
}
